import axios, {AxiosResponse} from "axios";
import {ResultEnum} from '@/enums/ResultEnum.ts';
import {ElMessage} from "element-plus";

// 创建 axios 实例
const service = axios.create({
    // baseURL: "http://203.135.96.9:9999",
    baseURL: "http://43.241.51.142:9999",
    // baseURL: "http://127.0.0.1:9999",
    timeout: 100000,
    headers: {"Content-Type": "application/json;charset=utf-8"},
});

// 响应拦截器
service.interceptors.response.use((response: AxiosResponse) => {
        if (response.config.responseType === "blob" ||
            response.config.responseType === "arraybuffer") {
            return response;
        }

        const {code, data, msg} = response.data;
        if (code === ResultEnum.SUCCESS || code === ResultEnum.OK) {
            return response.data;
        }
        console.error(msg || "系统出错");
        return Promise.reject(new Error(msg || "Error"));
    },
    (error: any) => {
        // 异常处理
        if (error.response && error.response.data) {
            const {msg} = error.response.data;
            ElMessage.error(msg || "系统出错");
        }
        return Promise.reject(error.message);
    }
)

// 导出 axios 实例
export default service;